package cn.nlsde.service.impl;

import cn.nlsde.constant.Constants;
import cn.nlsde.dao.CompanyMapper;
import cn.nlsde.entity.Company;
import cn.nlsde.service.CompanyService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * Created by zwj on 2016/10/6.
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    public final Logger logger = LoggerFactory.getLogger(CompanyServiceImpl.class);
    private static final String SPLIT = ",";
    private static final String LINE_SPLIT = "\r\n";
    @Autowired
    CompanyMapper companyMapper;

    @Override
    public int save(Company obj) {
        return 0;
    }

    @Override
    public int delete(Company obj) {
        return 0;
    }

    @Override
    public int update(Company obj) {
        return 0;
    }

    @Override
    public Company selectById(Integer id) {
        return null;
    }

    @Override
    public List<Company> selectAll() {
        return null;
    }

    @Override
    public int deleteById(Integer id) {
        return companyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public byte[] getCompanyBytemplateId(Integer templateId) {
        List<Company> companyList = companyMapper.selectOrdered(null, null, templateId);
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        sheet.setDefaultColumnWidth(20);
        sheet.setDefaultRowHeightInPoints(20);
        Row row = sheet.createRow(0);
        for (int i = 0; i < Constants.EXPORTEXCEL_TITLE.length; i++) {
            Cell cell = row.createCell(i);
            cell.setCellValue(Constants.EXPORTEXCEL_TITLE[i]);
        }
        //content
        for (int i = 1; i <= companyList.size(); i++) {
            row = sheet.createRow(i);
            Company company = companyList.get(i - 1);
            Cell cell = row.createCell(0);
            cell.setCellValue(company.getCompanyName());
            cell = row.createCell(1);
            cell.setCellValue(company.getInnovativeInputCapacity() * 100);
            cell = row.createCell(2);
            cell.setCellValue(company.getInnovationOutputCapacity() * 100);
            cell = row.createCell(3);
            cell.setCellValue(company.getInnovationPracticalCapacity() * 100);
            cell = row.createCell(4);
            cell.setCellValue(company.getInnovationFoundationCondition() * 100);
            cell = row.createCell(5);
            cell.setCellValue(company.getOtherCapacity() * 100);
            cell = row.createCell(6);
            cell.setCellValue((company.getInnovationFoundationCondition() + company
                    .getInnovationOutputCapacity() + company.getInnovationPracticalCapacity()
                    + company.getInnovativeInputCapacity() + company.getOtherCapacity()) * 100);
        }

        ByteArrayOutputStream bps = new ByteArrayOutputStream();
        try {
            workbook.write(bps);
        } catch (IOException e) {
            logger.info("{}", e.getMessage());
        }
        return bps.toByteArray();
    }

    @Override
    public byte[] getCompanyBytemplateCSV(Integer templateId) {
        List<Company> companyList = companyMapper.selectOrdered(null, null, templateId);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < Constants.EXPORTEXCEL_TITLE.length; i++) {
            sb.append(Constants.EXPORTEXCEL_TITLE[i] + SPLIT);
        }


        //content
        for (int i = 0; i < companyList.size(); i++) {
            sb.append(LINE_SPLIT);
            Company company = companyList.get(i);
            sb.append(company.getCompanyName() + SPLIT);
            sb.append(company.getInnovationOutputCapacity() * 100 + SPLIT);
            sb.append(company.getInnovationPracticalCapacity() * 100 + SPLIT);
            sb.append(company.getInnovationFoundationCondition() * 100 + SPLIT);
            sb.append(company.getOtherCapacity() * 100 + SPLIT);
            sb.append(company.getInnovativeInputCapacity() * 100 + SPLIT);
            sb.append((company.getInnovationFoundationCondition() + company
                    .getInnovationOutputCapacity() + company.getInnovationPracticalCapacity()
                    + company.getInnovativeInputCapacity() + company.getOtherCapacity()) * 100 + SPLIT);
        }
        byte[] bytes = new byte[0];
        try {
            bytes = sb.toString().getBytes("GBK");
        } catch (UnsupportedEncodingException e) {
            logger.error("{}", e.getMessage());
        }
        return bytes;
    }

    @Override
    public List<Company> selectOrdered(Integer offset, Integer limit, Integer templateId) {
        return companyMapper.selectOrdered(offset, limit, templateId);
    }

    @Override
    public List<Company> selectByCondition(Map map) {
        return companyMapper.selectByCondition(map);
    }
}
